package com.wwwarehouse.taskcenter.fragment.job_point.step2.child;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.time_pick.ChoosePickerDialog;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.JobPointTypeResponseBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_BUSINESS_ID;
import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_JOB_POINT_TYPE;
import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_JOB_POINT_UKID;
import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_IS_PARENT;
import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_WAREHOUSE_NAME;
import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_WAREHOUSE_UKID;

/**
 * Created by Jackie on 2017/10/27
 * 选择子作业点类型
 */

public class ChooseChildJobPointTypeFragment extends BaseFragment implements View.OnClickListener {
    private View mRootView;

    private LinearLayout mChooseJobPointLayout;
    private TextView mJobPointTypeTxt;

    private String mBusinessId;
    private boolean mIsParent;
    private String mJobPointUkid;
    private String mWarehouseUkid;
    private String mWarehouseName;

    private List<JobPointTypeResponseBean> mJobPointTypeList;
    private JobPointTypeResponseBean mJobPointTypeBean;

    private static final int REQUEST_GET_JOB_POINT_TYPE = 0;
    private static final int REQUEST_SAVE_JOB_POINT_TYPE = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_choose_child_job_point_type, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mChooseJobPointLayout = findView(mRootView, R.id.ll_choose_job_point);
        mJobPointTypeTxt = findView(mRootView, R.id.tv_job_point_type);
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            mBusinessId = bundle.getString(KEY_BUSINESS_ID);
            mIsParent = bundle.getBoolean(KEY_IS_PARENT);
            mJobPointUkid = bundle.getString(KEY_JOB_POINT_UKID);
            mWarehouseUkid = bundle.getString(KEY_WAREHOUSE_UKID);
            mWarehouseName = bundle.getString(KEY_WAREHOUSE_NAME);
        }
    }

    private void initEvent() {
        mChooseJobPointLayout.setOnClickListener(this);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseChildJobPointTypeFragment) {
            mActivity.setTitle(R.string.choose_job_point_type);
        }
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.ll_choose_job_point) {
            getJobPointType();
        }
    }

    NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {

        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_GET_JOB_POINT_TYPE:
                    if ("0".equals(commonClass.getCode())) {
                        mJobPointTypeList = JSON.parseArray(commonClass.getData().toString(), JobPointTypeResponseBean.class);

                        showJobPointDialog();
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                case REQUEST_SAVE_JOB_POINT_TYPE:
                    if ("0".equals(commonClass.getCode())) {
                        // 保存成功
                        Bundle bundle = new Bundle();

                        bundle.putString(KEY_BUSINESS_ID, mBusinessId);
                        bundle.putBoolean(KEY_IS_PARENT, mIsParent);
                        bundle.putString(KEY_JOB_POINT_UKID, mJobPointUkid);
                        bundle.putString(KEY_JOB_POINT_TYPE, mJobPointTypeTxt.getText().toString());
                        bundle.putString(KEY_WAREHOUSE_UKID, mWarehouseUkid);
                        bundle.putString(KEY_WAREHOUSE_NAME, mWarehouseName);

                        ChildJobPointDimensionListFragment childJobPointDimensionListFragment = new ChildJobPointDimensionListFragment();
                        childJobPointDimensionListFragment.setArguments(bundle);
                        pushFragment(childJobPointDimensionListFragment);
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            toast(result);
        }

        @Override
        public void onFinish(int what) {
            dismissProgressDialog();
        }
    };


    private void getJobPointType() {
        showProgressDialog();

        Map map = new HashMap();

        map.put("buId", mBusinessId);

        if (mIsParent) {
            map.put("type", "1");
        } else {
            map.put("type", "2");  //子作业点
        }

        NoHttpUtils.httpPost(TaskCenterConstant.URL_GET_JOB_POINT_TYPE, map, mOnResponseListener, REQUEST_GET_JOB_POINT_TYPE);
    }

    private void saveJobPointType(JobPointTypeResponseBean jobPointTypeBean) {
        showProgressDialog(getString(R.string.saving));

        Map map = new HashMap();

        map.put("jobPointUkid", mJobPointUkid);
        map.put("jobTypeUkid", jobPointTypeBean.getJbTypeUkid());

        NoHttpUtils.httpPost(TaskCenterConstant.URL_SAVE_JOB_POINT_TYPE, map, mOnResponseListener, REQUEST_SAVE_JOB_POINT_TYPE);
    }

    private void showJobPointDialog() {
        List<String> nameList = new ArrayList<>();

        for (JobPointTypeResponseBean jobPointTypeBean : mJobPointTypeList) {
            nameList.add(jobPointTypeBean.getJbTypeName());
        }

        new ChoosePickerDialog.Builder(mActivity).setData(nameList).setTitle(getString(R.string.choose_job_point_type)).setOnDataSelectedListener(new ChoosePickerDialog.OnSelectedListener() {
            @Override
            public void onSelected(String itemValue, int position) {
                mJobPointTypeTxt.setText(itemValue);

                mJobPointTypeBean = mJobPointTypeList.get(position);

                // 保存作业点类型
                saveJobPointType(mJobPointTypeBean);
            }

            @Override
            public void onCancel() {

            }
        }).create().show();
    }
}
